package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.PictureAdapter;
import com.sx.zongzhi.bean.DisputeBean;
import com.sx.zongzhi.bean.DisputeDetailBean;
import com.sx.zongzhi.presenter.DisputeDetailPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IDisputeDetailView;
import com.sx.zongzhi.widget.CustomDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 矛盾纠纷详情
 */
public class DisputeDetailActivity extends BaseActivity implements IDisputeDetailView{

    @BindView(R.id.ll_Prent) LinearLayout ll_Prent;
    @BindView(R.id.tv_EventName) TextView tv_EventName;
    //发生网格
    @BindView(R.id.et_HappenGrid) TextView et_HappenGrid;
    @BindView(R.id.et_HappenLocation) TextView et_HappenLocation;
    //发生日期
    @BindView(R.id.tv_Data) TextView tv_Data;
    //涉及单位
    @BindView(R.id.tv_InvolveCompany) TextView tv_InvolveCompany;
    //涉及人数
    @BindView(R.id.tv_InvolveNum) TextView tv_InvolveNum;
    //主要当事人
    @BindView(R.id.et_Litigant) TextView et_Litigant;
    @BindView(R.id.tv_EventType) TextView tv_EventType;
    @BindView(R.id.tv_EventScale) TextView tv_EventScale;
    @BindView(R.id.tv_EventSource) TextView tv_EventSource;
    @BindView(R.id.tv_EventDescribe) TextView tv_EventDescribe;
    @BindView(R.id.tv_CheckSuggest) TextView tv_CheckSuggest;
    //事件发展预测
    @BindView(R.id.tv_Forecast) TextView tv_Forecast;
    //调节措施
    @BindView(R.id.tv_Measures) TextView tv_Measures;
    @BindView(R.id.rv_imageRV) RecyclerView rvImageRV;
    private DisputeDetailPest detailPest;
    /**事件主键*/
    private DisputeBean.RowsBean rowsBean;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_dispute_detail);
        setTitleName("矛盾纠纷详情");
        rowsBean= (DisputeBean.RowsBean) getIntent().getSerializableExtra("DisputeBean.RowsBean");
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        detailPest=new DisputeDetailPest(this);
        if (rowsBean!=null){
            detailPest.getDisputeDetail(rowsBean.getId(),rowsBean.getIssueStepId(),rowsBean.getStepStatusCode());
        }else {
            ToastUtils.showToast(this,"详情获取失败 请检测网络");
        }
    }


    @Override
    public void getDisputeDetail(DisputeDetailBean detailBean) {
        if ("待办".equals(rowsBean.getListState())&&rowsBean.getStepStatusCode()==110){
            createHint();
        }
        if (detailPest!=null){
            try {
                tv_EventName.setText(detailBean.getName());
                et_HappenGrid.setText(detailBean.getOccurOrg().getOrgName());
                et_HappenLocation.setText(detailBean.getOccurPlace());
                tv_Data.setText(detailBean.getOccurDate());
                tv_InvolveCompany.setText(detailBean.getRelatedUnit());
                tv_InvolveNum.setText(detailBean.getRelatePeopleCount()+"");
                et_Litigant.setText(detailBean.getMainCharacters());
                tv_EventType.setText(detailBean.getIssueTypeName());
                tv_EventScale.setText(detailBean.getIssueKind().getDisplayName());
                tv_EventSource.setText(detailBean.getEventSource().getDisplayName());
                tv_EventDescribe.setText(detailBean.getIssueContent());
                tv_CheckSuggest.setText(detailBean.getEvaluateOpinion());
                tv_Forecast.setText(detailBean.getDevelopForecast());
                tv_Measures.setText(detailBean.getAdjustMeasures());
                if (detailBean.getFiles() != null && detailBean.getFiles() .size() > 0) {
                    rvImageRV.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, false));
                    PictureAdapter pictureAdapter = new PictureAdapter(this, detailBean.getFiles());
                    rvImageRV.setAdapter(pictureAdapter);
                }
                if (!BaseUtil.isEmpty(detailBean.getHandleOrg())){//事件化解
                    View defuseView= LayoutInflater.from(this).inflate(R.layout.dispute_detial_defuse,null);
                    ll_Prent.addView(defuseView);
                    ((TextView)defuseView.findViewById(R.id.tv_Time)).setText(detailBean.getHandleTime());
                    ((TextView)defuseView.findViewById(R.id.tv_Organizing)).setText(detailBean.getHandleOrg());
                    ((TextView)defuseView.findViewById(R.id.tv_EventPerson)).setText(detailBean.getHandleOwner());
                    ((TextView)defuseView.findViewById(R.id.tv_YesNo)).
                            setText(detailBean.isHandleSuccessed()+"");
                    ((TextView)defuseView.findViewById(R.id.tv_Contact)).setText(detailBean.getHandleOwnerPhone());
                    ((TextView)defuseView.findViewById(R.id.tv_DealWay)).
                            setText(detailBean.getResolvingWay().getDisplayName());
                    ((TextView)defuseView.findViewById(R.id.tv_DealState)).
                            setText(detailBean.getResolveSituation().getDisplayName());
                    ((TextView)defuseView.findViewById(R.id.tv_WorkWay)).setText(detailBean.getFollowWork());
                }
                if (detailBean.getIssueEvaluate()!=null){
                    DisputeDetailBean.IssueEvaluateBean evalBean=detailBean.getIssueEvaluate();
                    View evalView= LayoutInflater.from(this).inflate(R.layout.dispute_detial_evaluate,null);
                    ll_Prent.addView(evalView);
                    ((TextView)evalView.findViewById(R.id.tv_Time)).setText(evalBean.getEvaluateTime());
                    ((TextView)evalView.findViewById(R.id.tv_Class)).setText(evalBean.getEvaluateType()+"");
                    ((TextView)evalView.findViewById(R.id.tv_Content)).setText(evalBean.getEvaluateContent());
                    ((TextView)evalView.findViewById(R.id.tv_Forecast)).setText(evalBean.getAssessForecast());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 抄报时的弹窗
     */
    private void createHint() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("您已阅读 可在已办列表查看").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }
}
